package indi.MegaAstronic.cppUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;

public class HeaderToSingleFile {
	public static final String answerFileName="ans.txt";
	public static final String finalAnswer = "finalAns.txt";
	public static final String cppSrcName="cppSrc";
	
	public static void appendToAnswer(String currentFileName,PrintStream output,String[] allFileName) throws FileNotFoundException{
		Scanner input = new Scanner(new File(cppSrcName+File.separator+currentFileName));
		String temLine;
		boolean flag=true;
		
		while(input.hasNext()){
			flag=true;//flag=true代表可以直接写入
			temLine=input.nextLine();
			if(temLine.indexOf("H_INCLUDED")!=-1)flag=false;
			for (String headName : allFileName) {
				if(temLine.indexOf(headName)!=-1)flag=false;
			}
			if(TxtToCout.matchPaint(temLine)){
				flag=false;
				System.out.println("MATCH PAINT! "+temLine+" CurrentFileName = "+currentFileName);
				TxtToCout.fileToCout(output,TxtToCout.getPaintPath(temLine));
			}
			
			if(flag){
				output.append(temLine);
				output.append("\r\n");
			}
		}
		System.out.println(currentFileName+" done");
		input.close();
	}
	

	public static void main(String[] args) throws FileNotFoundException {
		File srcDir = new File(cppSrcName);
		PrintStream output = new PrintStream(new File(answerFileName));
		String allFileName[] = srcDir.list();
		/*
		 * swap myui -> last 
		 *
		 */
		int index = -1;
		for (int i=0;i<allFileName.length;i++) {
			if(allFileName[i].equalsIgnoreCase("MyUI.h"))
				index=i;
		}
		String tem=allFileName[index];
		for (int i=index;i<allFileName.length-1;i++) {
			allFileName[i]=allFileName[i+1];
		}
		allFileName[allFileName.length-1]=tem;
		//swap end
		
		for (String fileName : allFileName) {
			if(!fileName.equals("main.cpp"))
				appendToAnswer(fileName,output,allFileName);
		}
		
		/*
		 * main ->last
		 */
		System.out.println("----for Main----");
		appendToAnswer("main.cpp",output,allFileName);
		output.close();
	}

}
